package com.kaoqin.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.kaoqin.code.common.metatype.Dto;
import com.kaoqin.code.quartz.vo.QuartzVO;
import com.kaoqin.service.QuartzService;
import com.kaoqin.util.ResponseUtils;

/**
* @ClassName: QuartzController 
* @Description: 任务调度设置
* @author yunquan.hao 
* @date 2018年9月3日 下午5:42:32
 */
@RequestMapping("/quartz")
@Controller
public class QuartzController {
	@Autowired
	private QuartzService quartzService;
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	public void add(QuartzVO vo,HttpServletResponse response){
		try {
			int createJob = quartzService.createJob(vo);
			if(createJob != 0) {
				ResponseUtils.echoJson(response, 2000);
			}else {
				ResponseUtils.echoJson(response, 4001);
			}
		} catch (Exception e) {
			ResponseUtils.echoJson(response, 4000);
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="del",method=RequestMethod.DELETE)
	public void del(@RequestParam("jobId")Integer jobId,HttpServletResponse response){
		try {
			int job = quartzService.delJob(jobId);
			if(job != 0) {
				ResponseUtils.echoJson(response, 2000);
			}else {
				ResponseUtils.echoJson(response, 4001);
			}
		} catch (Exception e) {
			ResponseUtils.echoJson(response, 4000);
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="update",method=RequestMethod.PUT)
	public void update(QuartzVO vo,HttpServletResponse response){
		try {
			int job = quartzService.updateJob(vo);
			if(job != 0) {
				ResponseUtils.echoJson(response, 2000);
			}else {
				ResponseUtils.echoJson(response, 4001);
			}
		} catch (Exception e) {
			ResponseUtils.echoJson(response, 4000);
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String list(QuartzVO vo,HttpServletResponse response,Model model){
		PageInfo<Dto> page = quartzService.queryList(vo);
		model.addAttribute("List",page.getList());
		model.addAttribute("Page", page);
		return "quartz/list";
	}
}
